var seriesStr = new Array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
function editSeries(index){
	jQuery('#'+index).attr('innerHTML','');
	var input = '<input id="'+index+'_input" onblur="javascript:updateSeries(\''+index+'\')" type="text"/>';
	var i = jQuery('#edit').append(input);
}
function updateSeries(index){
	var value = jQuery('#'+index+'_input').attr('value');
	if(jQuery.trim(value)==''){
		alert('请输入商品系列!');
		jQuery('#'+index+'_input').focus();
		return;
	}
	jQuery('#'+index).attr('innerHTML',value);
	jQuery('#'+index+'_input').remove();
}
jQuery(document).ready(function(){
	var speed = 500;
	for(i=0;i<seriesStr.length;i++){
		if(Number(jQuery('#store_'+seriesStr[i]+'_lnum').attr('value'))>=0){
			jQuery('#store_'+seriesStr[i]).slideDown(Number(jQuery('#store_'+seriesStr[i]+'_lnum').attr('value'))*speed);
		}
	}
	
	for(i=0;i<seriesStr.length;i++){
		jQuery("#createShelves_"+seriesStr[i]).click(function(event){
			event = event? event: window.event;
			event = event? event:eventTag;
			var obj = event.srcElement ? event.srcElement:event.target; 
			event.stopPropagation();
			var offset = jQuery(obj).offset();
			jQuery('#indexShelves').attr('value',jQuery(this).attr('id').substr(jQuery(this).attr('id').length-1,1));
			jQuery('#shelvesPop').css({top:offset.top + jQuery(obj).height() + "px",left:offset.left});
			jQuery('#shelvesPop').slideDown(speed);
		})
		jQuery("#series_"+seriesStr[i]).click(function(event){
			event = event? event: window.event
					var obj = event.srcElement ? event.srcElement:event.target; 
			event.stopPropagation();
			var value = jQuery.trim(jQuery(obj).attr('innerHTML'));
			jQuery('#txtSeries').attr('value',value);
			jQuery('#indexSeries').attr('value',value);
			var offset = jQuery(obj).offset();
			jQuery('#indexSeries').attr('value',jQuery(this).attr('id'));
			jQuery('#seriesPop').css({top:offset.top + jQuery(obj).height() + "px",left:offset.left});
			jQuery('#seriesPop').slideDown(speed);
		 })
	}
	jQuery("#test").click(function(){
		jQuery('#store').slideDown(speed);
		jQuery('#store').slideUp(speed);
	})
	
	jQuery("#showall").click(function(event){
		if(!jQuery("#s").attr("checked")){
			for(i=0;i<seriesStr.length;i++){
				jQuery('#store_'+seriesStr[i]).slideDown(speed);
			}
		}else{
			for(i=0;i<seriesStr.length;i++){
				jQuery('#store_'+seriesStr[i]).slideUp(speed);
			}			
		}
	 })
	 
	jQuery("#showcheck").click(function(event){
		if(!jQuery("#a").attr("checked")){
			for(i=0;i<seriesStr.length;i++){
				if(jQuery('#checkbox_'+seriesStr[i]).attr("checked")){
					jQuery('#store_'+seriesStr[i]).slideDown(speed);
				}
			}
		}else{
			for(i=0;i<seriesStr.length;i++){
				if(jQuery('#checkbox_'+seriesStr[i]).attr("checked")){
					jQuery('#store_'+seriesStr[i]).slideUp(speed);
				}
			}			
		}
	 })
     
    jQuery("#detail").click(function(event){
        if(jQuery("#detail").text()=='查看店铺详情'){
            jQuery("#detail").text('关闭店铺详情');
            ThinkAjax.send(URL+'/updateOpenOrCloseForStore','ajax=1&store_detailflag=0&id='+jQuery("#store_id").val(),'','');
        }else{
            jQuery("#detail").text('查看店铺详情');
            ThinkAjax.send(URL+'/updateOpenOrCloseForStore','ajax=1&store_detailflag=1&id='+jQuery("#store_id").val(),'','');
        }
        jQuery("#detailc").slideToggle(500);
	 })
    
    KE.show({
        id : 'editmoretext',
    	resizeMode : 1,
        cssPath : '__PUBLIC__/Css/index.css',
        items : [
        'fontname', 'fontsize', '|', 'textcolor', 'bgcolor', 'bold', 'italic', 'underline',
        'removeformat', '|', 'justifyleft', 'justifycenter', 'justifyright', 'insertorderedlist',
        'insertunorderedlist', '|', 'emoticons', 'image', 'link']
    });
	
})
function editdetailmore(){
	jQuery("<div id='mask' style='width:100%;height:"+jQuery(document).height()+"px;position:absolute; background-color:#dfdfdf;left:0;top:0;z-index:2;'></div>").appendTo("body");   
	jQuery("#mask").css("opacity","0.6"); 
	jQuery('#editdetailmore').css({top:jQuery(window).height()/2,left:jQuery(document).width()/2-512});
	jQuery('#editdetailmore').show(600);
}

function hideEditmore(){
	jQuery("#mask").remove();
	jQuery("#editdetailmore").hide(600);
}

function editDetailmoresub(){
     ThinkAjax.sendForm('formDetail',URL+'/updateStoreDetail',completeEditDetail,'result');
}

function completeEditDetail(data,status,info){
	if(status==0){
		alert(info);
		return;
	}
	hideEditmore();
	location.reload(true); 
	
}

function hideEditmore(){
	jQuery("#mask").remove();
	jQuery("#editdetailmore").hide(600);
}
    
function cancelPop(div){
	var speed = 500;
	jQuery('#'+div).slideUp(speed);
	jQuery('#txtSeries').attr('value');
}


function updateSeries(){
	var value = jQuery('#txtSeries').attr('value');
	var series = jQuery('#indexSeries').attr('value');
	if(jQuery.trim(value)==''){
		alert('请输入商品系列!');
		jQuery('#txtSeries').focus();
		return;
	}
	ThinkAjax.send(URL+'/updateSeries','ajax=1&value='+value+'&series='+series+'&level=1',complete,'');
}
function complete(data,status,info){
	var speed = 500;
	if (status==1)
	{
		var seriescount = Number(jQuery('#seriescount').attr('innerHTML'));
		jQuery('#commoditycount').attr('innerHTML',seriescount+1);
		jQuery('#txtSeries').attr('value','');
		jQuery('#indexSeries').attr('value','');
		jQuery('#'+data.series).attr('innerHTML',data.value);
		jQuery('#seriesPop').slideUp(speed);
	}else{
		alert('更新商品系列失败！');
	}
}
function createShelves(){
	var speed = 500;
	var value = jQuery('#txtShelves').attr('value');
	var Shelves = jQuery('#indexShelves').attr('value');
	if((/^(\+|-)?\d+jQuery/.test( value )) || value < 0){
	    alert("货架数必须是正整数！"); 
	    jQuery('#txtShelves').focus();
	    return ;  
	}

	ThinkAjax.send(URL+'/addCommodity','ajax=1&count='+value+'&series='+Shelves+'&level=1',completeCreate,'');

}

function completeCreate(data,status,info){
	var speed = 500;
	var value = Number(data.count);
	var Shelves = jQuery('#indexShelves').attr('value');
	var snum  = Number(jQuery('#store_'+Shelves+'_snum').attr('value'));  //最后一行商品个数
	var lnum  = Number(jQuery('#store_'+Shelves+'_lnum').attr('value'));  //最大行索引
	var level = jQuery('#level').attr('value');
	var lacknum = snum==0?0:(5-snum);  //最后一行剩余空位
	var sunum = value-lacknum;//去除最后一样剩余的空位	
	
	if (status==0){
		alert(info);
		return;
	}
	
	if(snum!=0){
		strRemnant = '';
		for(i=0;i<(5-snum);i++){
			strRemnant += createTemplate(data.mcd_id[i]);
		}
		jQuery('#shelves_'+Shelves+'_'+lnum).append(strRemnant);
	}
	var result = parseInt(sunum/5);  //新建整行数
	var remnant = sunum%5;			//最后一行个数
	var lineCount = result;
	if(remnant!=0){
		lineCount += 1;
	}
	for(i=lnum+1;i<=lnum+lineCount;i++){
		if(i%2==0){
			str = '<div id="shelves_'+Shelves+'_'+i+'" style="width:100%;height:255px;background:#dfdfdd;">';
		}else{
			str = '<div id="shelves_'+Shelves+'_'+i+'" style="width:100%;height:255px;">';
		}
		str += '</div>';
		jQuery('#store_'+Shelves).append(str);
	}
	var tempLineCount = remnant==0?lnum+lineCount+1:lnum+lineCount;
	for(i=lnum+1,t=0;i<tempLineCount;i++,t++){
		var str = "";
		for(k=0,j=t*5+lacknum;(j<data.mcd_id.length)&&k<5;k++,j++){
			str += createTemplate(data.mcd_id[j]);
		}
		jQuery('#shelves_'+Shelves+'_'+i).append(str);
	}

	if(remnant!=0){
		strRemnant = '';
		for(i=result*5+lacknum;i<value;i++){
			strRemnant += createTemplate(data.mcd_id[i]);
		}
		jQuery('#shelves_'+Shelves+'_'+(lnum+lineCount)).append(strRemnant);
	}
	if(snum==0&&lnum==0&&value!=0){
		var seriescount = Number(jQuery('#seriescount').attr('innerHTML'));
		jQuery('#commoditycount').attr('innerHTML',seriescount+1);
	}
	var commoditycount_s = Number(jQuery('#commoditycount_'+Shelves).attr('innerHTML'));
	var commoditycount = Number(jQuery('#commoditycount').attr('innerHTML'));
	jQuery('#commoditycount').attr('innerHTML',commoditycount+value);
	jQuery('#commoditycount_'+Shelves).attr('innerHTML',commoditycount_s+value);
	alert(info);
	jQuery('#store_'+Shelves+'_lnum').attr('value',lnum+lineCount);
	jQuery('#store_'+Shelves+'_snum').attr('value',remnant);
	jQuery('#store_'+Shelves).slideDown(speed);
	jQuery('#shelvesPop').slideUp(speed);
}

function createTemplate(id){
	strTemplate = '<div id="'+id+'" class="mcd_id">';
	strTemplate += '    <div class="mcd_content"><a href="'+URL+'/showCommodity/mcd_mid/'+id+'/level/2"><span class="storein">点击进入货品上架</span></a></div>';
	strTemplate += '    <div class="canshu">';
	strTemplate += '        <ul>';
	strTemplate += '            <li>货号：</li>';
	strTemplate += '            <li>名称：</li>';
	strTemplate += '            <li>价格：</li>';
	strTemplate += '            <li>件数：</li>';
	strTemplate += '            <div class="canshu_delete"><a href="javascript:deleteStore(\''+id+'\')">删除</a></div>';
	strTemplate += '        </ul>';
	strTemplate += '    </div>';
	strTemplate += '</div>'; 
	return strTemplate;
}

function deleteStore(id){
	ThinkAjax.send(URL+'/deleteCommodity','ajax=1&id='+id,completeDelete,'');	
}

function completeDelete(data,status,info){
	if (status==0){
		alert(info);
		return;
	}
	jQuery("#"+data).remove();
	var Shelves = data.substr(-7,1);
	var commoditycount_s = Number(jQuery('#commoditycount_'+Shelves).attr('innerHTML'));
	var commoditycount = Number(jQuery('#commoditycount').attr('innerHTML'));
	jQuery('#commoditycount_'+Shelves).attr('innerHTML',commoditycount_s-1);
	jQuery('#commoditycount').attr('innerHTML',commoditycount-1);
	
}

function showOrHideSeries(sid,type,shelves){
	ThinkAjax.send(URL+'/showOrHideSeries','ajax=1&series_sid='+sid+'&type='+type+'&shelves='+shelves,completeShowOrHide,'');
}
function completeShowOrHide(data,status,info){
	if(status==0){
		alert(info);
		return;
	}
	var str = data.type=='1'?'<a href="javascript:showOrHideSeries(\''+data.series_sid+'\',\'0\',\''+data.shelves+'\')">显示</a>':'<a href="javascript:showOrHideSeries(\''+data.series_sid+'\',\'1\',\''+data.shelves+'\')">隐藏</a>';
	jQuery('#showOrHide_'+data.shelves).attr('innerHTML',str);
	
}

function showShelves(){
	var checkbox = jQuery("input:checked");
}

function search(){
	window.location = APP+"/Search?kw="+encodeURI(document.getElementById('kw').value);
}
